package com.milk.milkmedia.utils;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.milk.milkmedia.R;
import com.milk.milkmedia.bean.MusicInfo;
import java.util.List;


/**
 * Created by 微安 on 2015/7/27.
 */
public class MusicListAdapter extends BaseAdapter {

    private Context mContext;
    private List<MusicInfo> iMusicItems;
    private LayoutInflater mInflater;
    public class MusicListView{
        private ImageView imageView;
        private TextView musicTitle;
    }

    public MusicListAdapter(Context context, List<MusicInfo> musicInfos) {
        this.mContext = context;
        this.iMusicItems = musicInfos;
        mInflater = LayoutInflater.from(mContext);
    }

    @Override
    public int getCount() {
        return iMusicItems.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        MusicListView musicListView = null;
        if (convertView == null){
            musicListView = new MusicListView();
            convertView = mInflater.inflate(R.layout.item_musiclist,null);
            musicListView.imageView = (ImageView) convertView.findViewById(R.id.mlist_musictitle_img);
            musicListView.musicTitle = (TextView) convertView.findViewById(R.id.mlist_musictitle_tv);
            //设置控件集到convertView
            convertView.setTag(musicListView);
        }else
        {
            musicListView = (MusicListView) convertView.getTag();
        }
        musicListView.musicTitle.setText(iMusicItems.get(position).getmTitle());
        int random = position%2;
        switch (random){
            case 0 :
                musicListView.imageView.setBackgroundResource(R.mipmap.ic_musicbg01);
                break;
            case 1:
                musicListView.imageView.setBackgroundResource(R.mipmap.ic_musicbg02);
                break;
        }
        return convertView;
    }
}
